import 'dart:io';

import 'package:flutter_blue/flutter_blue.dart';

import 'ble_measure_plugin.dart';
import 'model/ble_device_state_model.dart';

typedef void OnDeviceStateListener(BleDeviceStateModel event);

class ConnectDeviceUtils {
  static FlutterBlue _flutterBlue = FlutterBlue.instance;
  static List<BluetoothDevice> devicesList = [];

  ///家测宝设备名称
  static String _temDeviceName = "IR Thermo";

  ///鱼跃血压仪名称
  static String _yuwellPressureName = "Yuwell BP";

  ///鱼跃血糖仪名称
  static String _yuwellBloodSugarName = "Yuwell Glucose";

  static String _tizhiDeviceName = 'AiLink_';

  ///😮爱奥乐血压名称
  static String _aiaolePressureName = "Bioland-BPM";

  ///三诺血压
  static String _sannuoPressureName = "ClinkBlood";

  ///乐普血氧仪
  static String _lepuOxygenName = "PC-60F";

  static List<String> _kangtaiNameList = ["TEMP04", "TEMP05", "TEMP06"];



  static void initMeasureDevice() {
    var map = <String, ScanResult>{};
    _flutterBlue.startScan();
    // if(Platform.isAndroid){
    //   BleMeasurePlugin.connectKangtai();
    // }
    _flutterBlue.scanResults.listen((results) {
      for (ScanResult r in results) {
        if (r.device.name.isNotEmpty) {
          if (!devicesList.contains(r.device)) {
            devicesList.add(r.device);
          }

          if (r.device.name.contains(_temDeviceName) &&
              BluetoothDeviceState.connected !=
                  BleMeasurePlugin.instance.temDeviceState) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectTemDevice(r.device);
            }
          }

          if (r.device.name.contains(_yuwellPressureName) &&
              BluetoothDeviceState.connected !=
                  BleMeasurePlugin.instance.yuyuePressureDeviceState) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectYuyuePresure(r.device);
            }
          }

          if (r.device.name.contains(_yuwellBloodSugarName) &&
              BluetoothDeviceState.connected !=
                  BleMeasurePlugin.instance.yuwellBloodMeasureState) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectYuyueBloodSugar(r.device);
            }
          }

          if (r.device.name.contains(_tizhiDeviceName) &&
              BluetoothDeviceState.connected !=
                  BleMeasurePlugin.instance.tizhiMeasureState) {
            if(map[r.device.name]==null){
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectTizhiDevice(r.device);
            }
          }

          if (r.device.name.contains(_aiaolePressureName)) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectAiaolePressute(r.device);
            }
          }

          if (r.device.name.contains(_sannuoPressureName)) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectSannuoPressure(r.device);
            }
          }

          if (r.device.name.contains(_lepuOxygenName)) {
            if(map[r.device.name]==null) {
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectLepuOxygen(r.device);
            }
          }
          if(_kangtaiNameList.contains(r.device.name)){
            if(map[r.device.name]==null){
              map[r.device.name] = r;
              BleMeasurePlugin.instance.connectKangtaiTemp(r.device);
            }

          }
        }
      }
    });
    _initBleStateListener();
  }

  static var stateModel = BleDeviceStateModel(
    temLink: false,
    pressureLink: false,
    bloodLink: false,
  );

  static void _initBleStateListener() {
    BleMeasurePlugin.instance.addDeviceStateListener(
        pressureStateHandler: (state) async {
      if (state == BluetoothDeviceState.connected) {
        stateModel.pressureLink = true;
      } else {
        stateModel.pressureLink = false;
      }
      // _deviceStateListener(stateModel);
      print("pressureStateHandler===>${state}");
      listeners.forEach((element) {
        element(stateModel);
      });
    }, temStateHandler: (state) async {
      if (state == BluetoothDeviceState.connected) {
        stateModel.temLink = true;
      } else {
        stateModel.temLink = false;
      }
      print("temStateHandler===>${state}");
      listeners.forEach((element) {
        element(stateModel);
      });
    }, bloodStateHandler: (state) async {
      if (state == BluetoothDeviceState.connected) {
        stateModel.bloodLink = true;
      } else {
        stateModel.bloodLink = false;
      }
      print("bloodStateHandler===>${state}");
      listeners.forEach((element) {
        element(stateModel);
      });
    }, tizhiStateHandler: (state) async {
      if (state == BluetoothDeviceState.connected) {
        stateModel.tizhiLink = true;
      } else {
        stateModel.tizhiLink = false;
      }
      print("tizhiStateHandler===>${state}");
      listeners.forEach((element) {
        element(stateModel);
      });
    }, oxygenStateHandler: (state) async {
      if (state == BluetoothDeviceState.connected) {
        stateModel.oxygenLink = true;
      } else {
        stateModel.oxygenLink = false;
      }
      print("oxygenStateHandler===>${state}");
      listeners.forEach((element) {
        element(stateModel);
      });
    });
  }

  // static OnDeviceStateListener _deviceStateListener;
  static List<OnDeviceStateListener> listeners = [];

  static void addOnDeviceStateListener(OnDeviceStateListener listener) {
    listeners.add(listener);
  }

  static void removeListener(OnDeviceStateListener listener) {
    listeners.remove(listener);
  }

  static void release() {
    BleMeasurePlugin.instance.release();
    listeners.clear();
    _flutterBlue.stopScan();
    // _flutterBlue = null;
  }
}
